const widgets = this.getApiPage().getApiWidgetByAliasName('三维组件');
const videoPageWidgets = this.getApiPage().getApiWidgetByAliasName('视频分页组件');
// 获取变量
var stream_url = this.getApiPage().getVariables().find(x => x.name == "stream_url").value;
const devices = this.pageObj.getApiWidgetByAliasName('devices');
const { dataSource } = devices.widgetObj._option
// const api = 'http://192.168.1.100:9098/basics-main/system/equipmentObject/listDetailByEquipmentCode'
const api = '/Basics/basics-main/system/equipmentObject/listDetailByEquipmentCode'
const params = { ...$event }

console.log('dataSource: => ', dataSource)
console.log('params: => ', params)

function createCamera(camera) {
  const { equipmentConfig, equipmentName } = camera
  const _camera = { ...camera, title: equipmentName }

  if (equipmentConfig && equipmentConfig?.gbVideoConfig) {
    const { sipUser, videoChannelId, sipServerHost, sipServerPort } = equipmentConfig.gbVideoConfig
    _camera.stream_path = `${sipUser}/${videoChannelId}`
    _camera.channelId = videoChannelId
    _camera.deviceId = sipUser
    _camera.host = `${sipServerHost}:${sipServerPort}`
  }

  return _camera
}

function resetDataSource(dataSource, rows) {
  return dataSource.map(item => {
    const newSet = new Set()
    item.catch = new Map()
    if (item.id === params.id && !item.catch.has(params.id)) {
      if (item.children) item.children.map(item => newSet.add(item.id))

      rows.forEach(camera => {
        if (!newSet.has(camera.id)) {
          const __camera = createCamera(camera)
          item.children.push(__camera)
        }
      })

      item.catch.set(params.id, rows)


      return item;
    } else {

      if (item.children) item.children = resetDataSource(item.children, rows)

      return item;
    }
  })
}

// 默认16分屏
widgets.widgetObj.sullivan.resizeSullivan(16)

const $wrapper = $(`#sullivan-display .swiper-wrapper`);
const wrapCls = $wrapper.data('class');
// 获取视频组件的分屏数并保存到window上
const gridSize = $(`#sullivan-display .swiper-wrapper .swiper-slide`).length;
window.videoCurrentGridSize = gridSize
let cls;
if (gridSize === 16) {
  cls = 'grid-4x4'
} else if (gridSize === 20) {
  cls = 'grid-5x4'
} else if (gridSize === 9) {
  cls = 'grid-3x3'
} else if (gridSize === 4) {
  cls = 'grid-2x2'
} else if (gridSize === 2) {
  cls = 'grid-1x1'
}
$wrapper.data('class', cls).removeClass(wrapCls).addClass(cls);


// `${api}?equipmentCode=camera&areaId=&pageSize=1&pageNum=1`
fetch(`${api}?equipmentTypeCode=camera&areaId=${params.id}`).then(res => {
  if (res.ok === true) {
    return res.json()
  }
}).then(res => {
  const { rows } = res.result
  const list = resetDataSource(dataSource, rows)
  // devices.widgetObj._option.dataSource = resetDataSource(dataSource, rows)
  // devices.widgetObj.render()

  console.log('vvvvvvvvvvvvvvvv', dataSource)



  // 清空已存在的摄像头
  widgets.widgetObj.sullivan.closeAll(true)

  // 获取当前区域节点下所有摄像头信息并存到window上
  const currentNode = list.filter(v => v.id === params.id)[0];
  // window.videoCurrentSelectArea = currentNode.children.filter(v => v.host);
  window.videoCurrentSelectArea = currentNode.children;
  videoPageWidgets.showWidget()
  videoPageWidgets.widgetObj.setCount(window.videoCurrentSelectArea.length)
  videoPageWidgets.widgetObj.setLimit(16)

  if (Array.isArray(window.videoCurrentSelectArea)) {
    // 设备当前分页数默认为1并保存到window上
    window.videoCurrentPagenum = 1
    console.log(gridSize, 'gridSize')

    // 根据当前分页和分屏获取当前要展示的视频
    const start = (window.videoCurrentPagenum - 1) * window.videoCurrentGridSize;
    const end = start + window.videoCurrentGridSize;
    const currentVideos = window.videoCurrentSelectArea.slice(start, end)
    currentVideos.map((v, i) => {
      console.log(v)
      if (v.host) {
        return widgets.widgetObj.sullivan.addPlayerWidthData(`${window.location.origin}/streamUrl/api/v1/stream/rtc/play`, { stream_path: v.stream_path, equipmentCode: v.equipmentCode, equipmentId: v.equipmentCode, ...v }, i)
      } else {
        return window.layer?.msg('摄像头未找到配置！');
      }
    })
  }
})